package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSONObject;
import com.turbo.boot.crawler.spider.worker.BaseTurboApiCrawler;
import com.turbo.utils.net.HttpHelper;
import com.xuxueli.crawler.rundata.RunData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 波罗的海交易指数
 *
 * @author wuyn
 * @since 2024/4/17
 */
@Slf4j
@Service("mysteelCrawler")
public class MysteelCrawler extends BaseTurboApiCrawler<MysteelCrawlerPageParser.PageVo> {

    private static final HttpHelper httpHelper = new HttpHelper();


    @Override
    public void crawl() {
        boolean buildResult = build();
        if (!buildResult) {
            return;
        }

        String url = xxlCrawlerBuilder.build().getRunData().getUrl();
        try {
            String result = httpHelper.get(url);
            if (StringUtils.isNotEmpty(result)) {

                JSONObject jsonObject = JSONObject.parseObject(result);
//                jsonObject.put("result", result);
                jsonObject.put("key", "HZB");
                jsonObject.put("name", "厚重板");
                this.parse(jsonObject);
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }


//    public boolean build() {
//        xxlCrawlerBuilder.setUrls("https://seo.mysteel.com/semapi/searchSemIndex/getValueBycode");
//        return true;
//    }


    @Override
    public String remoteCall(RunData runData) {
        return null;
    }


    @Override
    public void parse(JSONObject jsonObject) {

        String xzValueStr = jsonObject.getJSONObject("response").getString("xAxis");
        List<String> xzValues = JSONObject.parseArray(xzValueStr, String.class);

        JSONObject yzJson = (JSONObject) jsonObject.getJSONObject("response").getJSONArray("datas").get(0);

        List<String> yzValues = JSONObject.parseArray(yzJson.getString("yAxis"), String.class);

        MysteelCrawlerPageParser.PageVo pageVo = new MysteelCrawlerPageParser.PageVo();

        pageVo.setQuotaCode(jsonObject.getString("key"));
        pageVo.setQuotaName(jsonObject.getString("name"));
        pageVo.setXzValues(xzValues);
        pageVo.setYzValues(yzValues);

        getPageParser().parse(pageVo);
    }


    @Override
    public void parse(String result) {


    }


//    @Override
//    protected BaseTurboCrawlerPageParser<TksMysteelCrawlerPageParser.PageVo> getPageParser() {
//        return new TksMysteelCrawlerPageParser();
//    }


    public static void main(String[] args) {
        MysteelCrawler balticExchangeCrawler = new MysteelCrawler();
        balticExchangeCrawler.crawl();
    }
}
